package com.beordie.version_2.mvc;

import java.io.File;
import java.nio.channels.NonWritableChannelException;

/**
 * @Description
 * @Date 2022/2/17 10:07
 * @Created 30500
 */
public class BODViewResolver {
    /**
     * 视图后缀
     */
    private final String DEFAULT_TEMPLATE_SUFFIX = ".html";

    private File templateRootPath;

    public BODViewResolver(String fileName) {
        String file = this.getClass().getClassLoader().getResource(fileName).getFile();
        this.templateRootPath = new File(file);
    }

    public BODView resolverViewName(String viewName) {
        if (viewName == null || "".equals(viewName.trim())) { return null; }
        viewName = viewName.endsWith(DEFAULT_TEMPLATE_SUFFIX) ?
                viewName : (viewName + DEFAULT_TEMPLATE_SUFFIX);
        File file = new File((this.templateRootPath.getPath() + "/" + viewName)
                .replaceAll("/+", "/"));
        return new BODView(file);
    }
}
